<?php
class keu_list_umk_unit_lain_model extends MY_Model {
	var $model = 'keu_umk';
		
	function KeuJenisUMKPinjaman()
	{
		$ls_sql	= "	SELECT	KD_JENIS_UMK VALUE,
							NAMA_JENIS_UMK DISPLAY
					FROM	KEU_JENIS_UMK
					WHERE NVL(KD_JENIS_PINJAMAN,' ')!=' '
					ORDER BY DISPLAY";
					
		$result	= $this->db->query($ls_sql);
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return NULL;
		}
	}	
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT	UMK.ID_UMK, KAN.NAMA_KANTOR, CC.NAMA_COST_CENTER, 
							TO_CHAR(UMK.TGL_UMK, 'DD/MM/YYYY') TGL_UMK, UMK.JML_DANA_DIAJUKAN,
							UMK.KETERANGAN,
							UMK.JML_DANA_DISETUJUI,
							UMK.KD_JENIS_UMK,
							(SELECT keu_transaksi.id_dok
							  FROM    KEU_TRANS_KAS_BANK
								   INNER JOIN
									  KEU_TRANSAKSI
								   ON KEU_TRANSAKSI.ID_REFF = KEU_TRANS_KAS_BANK.ID_DOK
							 WHERE KEU_TRANS_KAS_BANK.ID_REFF = UMK.ID_UMK AND ROWNUM = 1) ID_DOK
					FROM	KEU_UMK UMK
							INNER JOIN SW_KANTOR KAN ON KAN.KD_KANTOR = UMK.KD_KANTOR
							INNER JOIN SW_COST_CENTER CC ON CC.ID_COST_CENTER_PK = UMK.KD_COST_CENTER
					WHERE	
						    NVL(UMK.STATUS_APPROVE,'P') <> 'D' AND
							KD_JENIS_UMK IN (SELECT	KD_JENIS_UMK
											FROM	KEU_JENIS_UMK
											WHERE NVL(KD_JENIS_PINJAMAN,' ')=' ') AND
							UMK.TGl_UMK BETWEEN 
							TO_DATE('".date_format3(post('date_from'), 'YYYY-MM-DD')."','YYYY-MM-DD') AND 
							TO_DATE('".date_format3(post('date_to'), 'YYYY-MM-DD')."','YYYY-MM-DD') AND					
							(
								UPPER(UMK.ID_UMK) LIKE UPPER('%".post('keyword')."%') OR
								UPPER(KAN.NAMA_KANTOR) LIKE UPPER('%".post('keyword')."%') OR
								UPPER(CC.NAMA_COST_CENTER) LIKE UPPER('%".post('keyword')."%') OR
								UPPER(UMK.KETERANGAN)	LIKE UPPER('%".post('keyword')."%')	
							)
					ORDER BY ".(post('sort')=='' ? 'UMK.ID_UMK' : post('sort'))." ".(post('order')=='' ? 'ASC' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
	
	
		
	function Add($arr_data){
		$this->db->insert('KEU_UMK', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('KEU_UMK', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_code){
		$result = $this->db->get_where('KEU_UMK', array('ID_UMK' => $as_code));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_code)
	{
		$ls_sql	= "	SELECT	ID_UMK, KD_KANTOR, KD_COST_CENTER, TO_CHAR(TGL_UMK,'DD/MM/YYYY') TGL_UMK, KETERANGAN, JML_DANA_DIAJUKAN, KD_VALUTA, KURS,
							 TO_CHAR(TGL_APPROVE,'DD/MM/YYYY') TGL_APPROVE, KET_APPROVE, STATUS_APPROVE, JML_DANA_DISETUJUI, 
							 KD_JENIS_UMK
					FROM	KEU_UMK
					WHERE	ID_UMK = '".$as_code."'";
							
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	
	function DeleteSingle($as_code)
	{
		try {
			$this->db->delete('KEU_UMK_DETAIL', array('ID_UMK' => $as_code));
			$this->db->delete('KEU_UMK', array('ID_UMK' => $as_code));
			
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
	
}
?>